package cn.zwx.learn.basic.lock;

/**
 * @description: 测试类
 * @projectName:java-basic
 * @see:cn.zwx.learn.basic
 * @author:zhangwenxue
 * @createTime:2020/12/10 12:37
 * @version:1.0
 */
public class LockTest {

    private int i = 0;

    MyLock myLock = new MyLock();

    private void add() {
        myLock.lock();
        try {
            i++;
        } finally {
            myLock.unLock();
        }
    }

    /**
     * @description  此时用不同线程去调用add方法 如果返回结果稳定是4000则说明我们保证有序性和可见性
     * 如果没有则表示我们没有解决此问题
     **/
    public static void main(String[] args) throws InterruptedException {
        LockTest lockTest = new LockTest();
        for (int j = 0; j < 4; j++) {
            new Thread(() -> {
                for (int k = 0; k < 1000; k++) {
                    lockTest.add();
                }
            }).start();
        }
        Thread.sleep(2000);
        System.out.println("结果：" + lockTest.i);
    }

}
